package cn.otrue.patienthealthmanager.adapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.otrue.patienthealthmanager.R;
import cn.otrue.patienthealthmanager.bean.SugarTemplateMonitorDetail;
import cn.otrue.patienthealthmanager.utils.MyConst;

public class WeekTableListAdpater extends BaseAdapter implements
		OnClickListener {
	private int type = MyConst.CHANGE_PLAN;
	private List<SugarTemplateMonitorDetail> data;
	private LayoutInflater inflater;
	private Context context;
	private ImageView iv_week_lv_1;
	private ImageView iv_week_lv_2;
	private ImageView iv_week_lv_3;
	private ImageView iv_week_lv_4;
	private ImageView iv_week_lv_5;
	private ImageView iv_week_lv_6;
	private ImageView iv_week_lv_7;
	private ImageView iv_week_lv_8;

	public WeekTableListAdpater(Context context,
			List<SugarTemplateMonitorDetail> detailData) {
		super();
		this.data = detailData;
		this.context = context;
		inflater = LayoutInflater.from(this.context);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return data.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		// TODO Auto-generated method stub
		arg1 = inflater.inflate(R.layout.item_week_table_lv, null);
		TextView tv_week_lv_week = (TextView) arg1
				.findViewById(R.id.tv_week_lv_week);
		LinearLayout ll_week_table = (LinearLayout) arg1
				.findViewById(R.id.ll_week_table);

		AbsListView.LayoutParams params = new AbsListView.LayoutParams(
				MyConst.width, (MyConst.width - 8) / 9);
		ll_week_table.setLayoutParams(params);

		if (arg0 % 2 == 0) {
			ll_week_table.setBackgroundColor(Color.parseColor("#FDE7E7"));
		} else {
			ll_week_table.setBackgroundColor(Color.parseColor("#FAD9D9"));
		}

		iv_week_lv_1 = (ImageView) arg1.findViewById(R.id.iv_week_lv_1);
		iv_week_lv_2 = (ImageView) arg1.findViewById(R.id.iv_week_lv_2);
		iv_week_lv_3 = (ImageView) arg1.findViewById(R.id.iv_week_lv_3);
		iv_week_lv_4 = (ImageView) arg1.findViewById(R.id.iv_week_lv_4);
		iv_week_lv_5 = (ImageView) arg1.findViewById(R.id.iv_week_lv_5);
		iv_week_lv_6 = (ImageView) arg1.findViewById(R.id.iv_week_lv_6);
		iv_week_lv_7 = (ImageView) arg1.findViewById(R.id.iv_week_lv_7);
		iv_week_lv_8 = (ImageView) arg1.findViewById(R.id.iv_week_lv_8);

		SugarTemplateMonitorDetail eachDetail = data.get(arg0);
		tv_week_lv_week.setText(eachDetail.getWeekend() + "");

		Map<String, String> map = new HashMap<String, String>();
		map.put("week", arg0 + "");
		map.put("state", eachDetail.getBefore_drawn() + "");
		iv_week_lv_1.setTag(map);
		map.put("state", eachDetail.getEmpty_stomach() + "");
		iv_week_lv_2.setTag(map);
		map.put("state", eachDetail.getAfter_breakfast() + "");
		iv_week_lv_3.setTag(map);
		map.put("state", eachDetail.getBefore_lunch() + "");
		iv_week_lv_4.setTag(map);
		map.put("state", eachDetail.getAfter_lunch() + "");
		iv_week_lv_5.setTag(map);
		map.put("state", eachDetail.getBefore_dinner() + "");
		iv_week_lv_6.setTag(map);
		map.put("state", eachDetail.getAfter_dinner() + "");
		iv_week_lv_7.setTag(map);
		map.put("state", eachDetail.getBefore_sleep() + "");
		iv_week_lv_8.setTag(map);

		setSrc(iv_week_lv_1, eachDetail.getBefore_drawn() + "");
		setSrc(iv_week_lv_2, eachDetail.getEmpty_stomach() + "");
		setSrc(iv_week_lv_3, eachDetail.getAfter_breakfast() + "");
		setSrc(iv_week_lv_4, eachDetail.getBefore_lunch() + "");
		setSrc(iv_week_lv_5, eachDetail.getAfter_lunch() + "");
		setSrc(iv_week_lv_6, eachDetail.getBefore_dinner() + "");
		setSrc(iv_week_lv_7, eachDetail.getAfter_dinner() + "");
		setSrc(iv_week_lv_8, eachDetail.getBefore_sleep() + "");

		setListener();
		return arg1;
	}

	public void setType(int type) {
		this.type = type;
	}

	public void setListener() {
		if (type != MyConst.SHOW_PLAN) {
			iv_week_lv_1.setOnClickListener(this);
			iv_week_lv_2.setOnClickListener(this);
			iv_week_lv_3.setOnClickListener(this);
			iv_week_lv_4.setOnClickListener(this);
			iv_week_lv_5.setOnClickListener(this);
			iv_week_lv_6.setOnClickListener(this);
			iv_week_lv_7.setOnClickListener(this);
			iv_week_lv_8.setOnClickListener(this);
		} else {
			iv_week_lv_1.setOnClickListener(new MyClickListener());
			iv_week_lv_2.setOnClickListener(new MyClickListener());
			iv_week_lv_3.setOnClickListener(new MyClickListener());
			iv_week_lv_4.setOnClickListener(new MyClickListener());
			iv_week_lv_5.setOnClickListener(new MyClickListener());
			iv_week_lv_6.setOnClickListener(new MyClickListener());
			iv_week_lv_7.setOnClickListener(new MyClickListener());
			iv_week_lv_8.setOnClickListener(new MyClickListener());
		}
	}

	public class MyClickListener implements OnClickListener {

		@Override
		public void onClick(View v) {

		}

	}

	private void setSrc(ImageView iv, String state) {

		if (state.equals("1")) {
			iv.setImageResource(R.drawable.week_01);
		} else if (state.equals("2")) {
			iv.setImageResource(R.drawable.week_02);
		} else {
			iv.setImageResource(R.drawable.week_00);
		}
	}

	@Override
	public void onClick(View v) {

		Map<String, String> tag = (Map<String, String>) v.getTag();
		if (!TextUtils.isEmpty(tag.get("state"))) {
			int state = Integer.parseInt(tag.get("state"));
			state++;
			setSrc((ImageView) v, state % 3 + "");
			tag.put("state", state + "");
			v.setTag(tag);

			int week = Integer.parseInt(tag.get("week"));
			switch (v.getId()) {
			case R.id.iv_week_lv_1:
				data.get(week).setBefore_drawn(state);
				break;
			case R.id.iv_week_lv_2:
				data.get(week).setEmpty_stomach(state);
				break;
			case R.id.iv_week_lv_3:
				data.get(week).setAfter_breakfast(state);
				break;
			case R.id.iv_week_lv_4:
				data.get(week).setBefore_lunch(state);
				break;
			case R.id.iv_week_lv_5:
				data.get(week).setAfter_lunch(state);
				break;
			case R.id.iv_week_lv_6:
				data.get(week).setBefore_dinner(state);
				break;
			case R.id.iv_week_lv_7:
				data.get(week).setAfter_dinner(state);
				break;
			case R.id.iv_week_lv_8:
				data.get(week).setBefore_sleep(state);
				break;

			default:
				break;
			}

		}

	}
}
